<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");
require_once("./_inc/frameFunction.php");

$errMsg = "";
$process_unit = sxREQ('pu');
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'pu'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save"){
	$errMsg .= frameIns();
}
sxRDef('display_only',"N");

$sSQL = "SELECT l.* FROM layout l " .
			" WHERE l.module='".substr($process_unit,0,4)."' " .
			" ORDER BY l.layout";
$objSDAO->query($sSQL, "lay");

$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));

echo getPuHeader($process_unit);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("Salva i dati del nuovo frame"),
										"text"=>_t("salva")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0)
										)
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('pu');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>Frame</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".getHtmlText('frame',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Layout*</td>" .
			"<td class=\"text01\">");
$aValL = array(''=>'-- select a layout');
while($row = $objSDAO->fetch("lay")){
	$aValL[$row["layout"]] = $row["layout"].' - '.$row["description"];
}
echo getHtmlSelect('layout', $aValL);
echo("</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
$aValYN = array('Y'=>'Yes','N'=>'No');
echo("<tr>" .
			"<td class=\"labl01\">Display only</td>" .
			"<td class=\"text01\">" .
			getHtmlRadio('display_only',$aValYN).
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',80,3)."</td>" .
		"</tr>\n");
echo("</table>");
echo $sCmd;
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function frameIns($redirOnEnd = true){
 	global $objSDAO;
	$errMsg = "";
	if(sxREQ("frame") == "")
		$errMsg .= "missing frame<br>";
	if(preg_match("/[^0-9a-zA-Z_]",sxREQ("frame")))
		$errMsg .= "frame: only letter, numbers and _<br>";
	if(sxREQ("layout") == "")
		$errMsg .= "unselected layout<br>";
	$sSQL = "SELECT * FROM frame " .
			"WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS('frame')."' ";	
	$objSDAO->query($sSQL);
	if ($aX=$objSDAO->fetch()) {
		$errMsg .= "existing frame<br>";
	}
	if($errMsg != "")
		return $errMsg;
		
	//existence
	$sSQL = "SELECT count(*) as num FROM frame "
		." WHERE process_unit='".sxRAS('pu')."' AND frame='".sxRAS("frame")."'";
	$num = 0;
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= _t("existing frame");
	if($errMsg != ""){ return $errMsg; }		
	
	$aAliasPU = array();
  	$aAliasLay = array();
  	//load pu alias
	$sSQL = "SELECT * FROM pu_alias WHERE process_unit='$process_unit' ORDER BY alias, entity";
	$objSDAO->query($sSQL,"qry");
	while($row = $objSDAO->fetch("qry")){
		$aAliasPU[$row["alias"]] = $row["entity"]; 
	}

	//load layout alias
	$sSQL = "SELECT * FROM layout_alias WHERE layout='$layout' ORDER BY layout_alias, entity";
	$objSDAO->query($sSQL,"qry");
	while($row = $objSDAO->fetch("qry")){
		$aAliasLay[$row["layout_alias"]] = $row["entity"]; 
	}

	$aUsedPuAlias = array();
	foreach($aAliasLay as $aliasLay=>$entityLay){
		if (ereg("[WX]",$aliasLay)) {
			continue;
		}
		$associato = false;
		foreach($aAliasPU as $aliasPU=>$entityPU){
			if($entityPU == $entityLay && !in_array($aliasPU, $aUsedPuAlias)){
				$aUsedPuAlias[] = $aliasPU;
				$associato = true;
				break;;
			}
		}
		if(!$associato){
			$errMsg .= "layout alias $aliasLay unmatchable.<br>";
		}
	}
	if($errMsg != ""){ return $errMsg; }		
	
	$sSQL = "INSERT INTO " .
			"frame " .
			"(" .
			"process_unit, " .
			"frame, " .
			"layout, " .
			"description, " .
			"display_only, " .
			"note" .
			")" .
			"VALUES(" .
			"'".sxRAS('pu')."', " .
			"'".sxRAS("frame")."', " .
			"'".sxRAS("layout")."', " .
			"'".sxRAS("description")."'," .
			"'".sxRAS("display_only")."'," .
			"'".sxRAS("note")."'" .
			")";		
	$objSDAO->exec($sSQL);
	foreach($aAliasLay as $aliasLay=>$entityLay){
		if (ereg("[WX]",$aliasLay)) {
			$sSQL = "INSERT INTO frame_alias_match (process_unit,frame,alias,layout,layout_alias)" .
						"VALUES('".sxRAS('pu')."', '".sxRAS('frame')."', '$aliasLay', '".sxRAS("layout")."', '$aliasLay')";
			$objSDAO->exec($sSQL);
			continue;
		}
		$associato = false;
		foreach($aAliasPU as $aliasPU=>$entityPU){
			if($entityPU == $entityLay && !in_array($aliasPU, $aUsedPuAlias)){
				$aUsedPuAlias[] = $aliasPU;
				$associato = true;
				$sSQL = "INSERT INTO frame_alias_match " .
						"(process_unit, " .
						"frame, " .
						"alias, " .
						"layout, " .
						"layout_alias)" .
						"VALUES" .
						"('".sxRAS("pu")."', " .
						"'".sxRAS("frame")."', " .
						"'$aliasPU', " .
						"'".sxRAS("layout")."', " .
						"'$aliasLay')";
				$objSDAO->exec($sSQL);
				break;;
			}
		}
	}
	//copy memory variable
	$sSQL = "SELECT * FROM layout_variable WHERE layout = '".sxRAS("layout")."'";
	$objSDAO->query($sSQL,'ftv');
	while($row=$objSDAO->fetch('ftv')) {
		$sSQL = "INSERT INTO pu_variable (process_unit, alias, variable, description, note, read_only) " .
				" values('".sxRAS('process_unit')."'," .
				" '".sxAS($row['layout_alias'])."', " .
				" '".sxAS($row['variable'])."', " .
				" '".sxAS($row['variable'])."', " .
				" '', " .
				" 'N')";
		$objSDAO->exec($sSQL,array('contOnErr'=>true));
	}
	serverRedir(SERVER_URL_ROOT."/app/proc/fram/frameMod.php?pu=".sxREQ('pu')."&frame=".sxREQ('frame')."&xsn=".XSN);
	return 'abnormal end';
}
